var e = require;
var t = module;
var o = exports;
Object.defineProperty(o, "__esModule", {value: !0});
var n = e("BaseModuleUI"),
    a = e("GlobalParams"),
    i = e("CommonHelper"),
    r = e("PureHelper"),
    s = e("Game"),
    l = e("node_team"),
    c = e("role_node"),
    d = e("team_node_info"),
    h = e("tower_node_info"),
    _ = cc._decorator,
    u = _.ccclass,
    p = _.property,
    m = (function (e) {
        function t() {
            var t = (null !== e && e.apply(this, arguments)) || this;
            return (
                (t.soldier_skin = null),
                (t.team_info = null),
                (t.target_node = null),
                (t.soldier_kill_id = 0),
                (t.soldier_rigidbody = null),
                t
            );
        }
        return (
            __extends(t, e),
            (t.prototype.initView = function () {
                (this.team_info = this.node.getComponent(d.default)),
                    (this.node.scaleX = this.team_info.team_info == s.TeamInfo.left_team ? -1 : 1),
                    (this.soldier_kill_id = this.team_info.soldier_kill_id),
                    (this.soldier_rigidbody = this.node.getComponent(cc.RigidBody)),
                    this.soldierMove();
            }),
            (t.prototype.soldierMove = function () {
                var e = this;
                if (a.GlobalParams.GameStart) {
                    for (var t = [], o = 9999, n = 0, i = this.node.parent.children; n < i.length; n++) {
                        var l = i[n];
                        if (l.active && this.getSoliderInfo(l)) {
                            var c = Math.abs(r.default.Distance(this.node.getPosition(), l.getPosition()));
                            if (c < 400) {
                                var d = {node: l, dis: c};
                                t.push(d), (o = o < c ? o : c);
                            }
                        }
                    }
                    var h = new cc.Vec2(0, 0);
                    t.length < 1
                        ? (h.x = this.team_info.team_info == s.TeamInfo.left_team ? 1 : -1)
                        : (t.length > 1 &&
                              t.sort(function (e, t) {
                                  return e.dis - t.dis;
                              }),
                          (this.target_node = t[0].node),
                          r.default.Angle_X(this.target_node.getPosition(), this.node.getPosition()),
                          (h = cc
                              .v2(this.target_node.x, this.target_node.y)
                              .sub(cc.v2(this.node.x, this.node.y))
                              .normalize()));
                    var _ = this.node.getPosition().add(h.mul(10));
                    (this.node.scaleX = this.node.x > _.x ? 1 : -1),
                        o < this.team_info.node_info.ma_range
                            ? Math.abs(this.target_node.y - this.node.y) > 50
                                ? ((_ = this.node
                                      .getPosition()
                                      .add(cc.v2(0, this.target_node.y > this.node.y ? 1 : -1).mul(10))),
                                  "run" != this.soldier_skin.animation &&
                                      (this.soldier_skin.setAnimation(0, "run", !0),
                                      (this.soldier_skin.timeScale = 0.5)),
                                  cc
                                      .tween(this.node)
                                      .to(a.GlobalParams.GameUpdateTime, {position: cc.v3(_.x, _.y, 0)})
                                      .call(function () {
                                          e.soldier_rigidbody.syncPosition(!0), e.soldierMove();
                                      })
                                      .start())
                                : this.attackFoeSoldier()
                            : ("run" != this.soldier_skin.animation &&
                                  (this.soldier_skin.setAnimation(0, "run", !0), (this.soldier_skin.timeScale = 0.5)),
                              cc
                                  .tween(this.node)
                                  .to(a.GlobalParams.GameUpdateTime, {position: cc.v3(_.x, _.y, 0)})
                                  .call(function () {
                                      e.soldier_rigidbody.syncPosition(!0), e.soldierMove();
                                  })
                                  .start());
                }
            }),
            (t.prototype.getSoliderInfo = function (e) {
                var t = e.name;
                return (
                    t == "soldier_team_" + (this.team_info.team_info == s.TeamInfo.left_team ? 2 : 1) ||
                    ("role_team" == t.substring(0, 9) &&
                        this.team_info.team_info % 2 == Number(i.default.getNameType(t, "role_team_")) % 2) ||
                    ("tower" == t.substring(0, 5) &&
                        this.team_info.team_info != e.getComponent(l.default).getTeamInfo())
                );
            }),
            (t.prototype.attackFoeSoldier = function () {
                var e = this;
                this.soldier_skin.setAnimation(0, "attack", !1),
                    (this.soldier_skin.timeScale = 1),
                    this.soldier_skin.setCompleteListener(function () {
                        e.soldier_skin.setCompleteListener(null),
                            e.target_node.isValid &&
                                ("soldier_team_" == e.target_node.name.substring(0, 13)
                                    ? e.target_node
                                          .getComponent(d.default)
                                          .soldierBeHit(e.team_info.node_info.ma_attack, e.soldier_kill_id)
                                    : "role_team" == e.target_node.name.substring(0, 9)
                                    ? e.target_node
                                          .getComponent(c.default)
                                          .roleBeHit(e.team_info.node_info.ma_attack, e.soldier_kill_id)
                                    : "tower" == e.target_node.name.substring(0, 5) &&
                                      e.target_node
                                          .getComponent(h.default)
                                          .towerBeHit(e.team_info.node_info.ma_attack, e.soldier_kill_id)),
                            e.scheduleOnce(function () {
                                e.soldierMove();
                            }, a.GlobalParams.GameUpdateTime);
                    });
            }),
            (t.prototype.onBeginContact = function (e, t, o) {
                var n = o.node.group;
                ("role" != n && "soldier" != n) || (e.disabled = !0);
            }),
            __decorate([p(sp.Skeleton)], t.prototype, "soldier_skin", void 0),
            __decorate([u], t)
        );
    })(n.default);
o.default = m;
